<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Admin_User extends Controller_Admin_Base
{
    public function action_index()
    {   
        $company = ORM::factory('company')->get_current();
        
        $users = ORM::factory('user')->get_users($company);
        
        $content = new View('admin/user/index_tpl');
        $content->users = $users;
        
        $this->print_template('Пользователи', $content, 'settings');     
    }
    
    public function action_add()
    { 
        if (!Model_User::check_role(Model_User::ROLE_ADMIN)) {
            throw new Controller_Exception('У вас нет прав');   
        }
        
        if (isset($_GET['check_email'])) {
            $email  = $_GET['email'];
            
            if (Model_User::check_email($email)) {
                echo 'true';
                die();    
            }  
            echo 'false';
            die();
        }
        
        if (HTTP_Request::POST == $this->request->method()) {
            $role = $this->request->post('role');
            $name = $this->request->post('name');
            /*$fname = $this->request->post('fname');
            $lname = $this->request->post('lname');*/ 
            $email = $this->request->post('email');
            $send_invite  = $this->request->post('sendInvite');
                  
            try {        
                $user = ORM::factory('user')->add_user_by_invite($email, $name, $role, $send_invite);

                Request::current()->redirect(Helper_Admin::user_url()); 
            } catch (Model_Exception $e) {
                
            }
        }
       
        $content = new View('admin/user/add_tpl');       
        $this->print_template('Добавить пользователя', $content, 'settings');   
    }
    
    public function action_profile()
    {  
        $user_id = $this->request->param('id') ? $this->request->param('id') : null;
        if ($user_id) {
            $user = ORM::factory('user')->filter($user_id); 
        } else {
            $user = Model_Auth::instance()->get_user();
        }
        
        if (HTTP_Request::POST == $this->request->method()) {
            $role = $this->request->post('role');
            $name = $this->request->post('name');
            $email = $this->request->post('user_email');
            $password = $this->request->post('password');
                  
            try { 
                $user->edit_user($email, $name, $role, $password);       
            } catch (Model_Exception $e) {
                
            }
        } 
        
        $content = new View('admin/user/profile_tpl');
        $content->user = $user;
               
        $this->print_template('Редактирование профиля - '.$user->name, $content, 'settings');  
    }
    
    public function action_notifications()
    {
        $user_id = $this->request->param('id') ? $this->request->param('id') : null;
        if ($user_id) {
            $user = ORM::factory('user')->filter($user_id); 
        } else {
            $user = Model_Auth::instance()->get_user();
        }
        
        if (HTTP_Request::POST == $this->request->method()) {
            $notifications = $this->request->post('notifications');
            $all_notifications = Model_User::get_all_notifications();
            
            try { 
                foreach ($all_notifications as $notification) {
                    if (is_array($notifications) && in_array($notification, $notifications)) {
                        $user->set_notifications($notification, 1);        
                    } else {
                        $user->set_notifications($notification, 0);   
                    }    
                }
            } catch (Model_Exception $e) {
                
            }
        } 
        
        $content = new View('admin/user/notifications_tpl');
        $content->user = $user;
               
        $this->print_template('Уведомления - '.$user->name, $content, 'settings');    
    }
    
    public function action_delete()
    {  
        $user_id = $this->request->param('id') ? $this->request->param('id') : null;
        $user = ORM::factory('user')->filter($user_id); 
            
        if (!Model_User::check_role_by_user($user)) {
            throw new Controller_Exception('У вас нет прав');   
        }
        
        $user->mark_user_as_deleted();
        Request::current()->redirect(Helper_Admin::user_url());
    }

} 
